<?php
        include_once('../inc/global.php');
        session_start();
        
        
function IsLoggedIn()
{
        
        return isset($_SESSION['UserID']);
}

function GetUserName()
{
        return $_SESSION['UserName'];
}

function GetUserID()
{
        return $_SESSION['UserID'];
}

function DoLogin($email, $password)
{
        $sql =  "SELECT P.id, P.FirstName, P.LastName, P.Password "
                .       "From User P WHERE P.email = '$email'";
                //echo $sql;
                
        $conn = getConnection();
        $result = $conn->query($sql);
        $rs = $result->fetch_assoc();
        $conn->close();
        if($rs)
        {
                if($rs['Password'] == $password)
                {
                        $_SESSION['UserID'] = $rs['id'];
                        $_SESSION['UserName'] = $rs['FirstName'] . " " . $rs['LastName'];
                        $_SESSION['UserEmail'] = $email;
                        $user= true;
                        return true;
                }
        }
        return false;   
}
function DoLogin1($email, $password)
{
        $sql =  "SELECT P.id, P.FirstName, P.LastName, P.Password "
                .       "From Admin P WHERE P.email = '$email'";
                //echo $sql;
        $conn = getConnection();
        $result = $conn->query($sql);
        $rs = $result->fetch_assoc();
        $conn->close();
        if($rs)
        {
                if($rs['Password'] == $password)
                {
                        $_SESSION['UserID'] = $rs['id'];
                        $_SESSION['UserName'] = $rs['FirstName'] . " " . $rs['LastName'];
                        $_SESSION['UserEmail'] = $email;
                        $user= false;
                        return true;
                }
        }
        return false;   
}


function DoLogout()
{
        session_destroy();
}

function RequireLogin()
{
        if(!IsLoggedIn())
        {
                header('location: login.php');
                die();
        }
} 

function GetCart()
{
        
        $conn = getConnection();
        $sql =  'SELECT * FROM Cart'; 
        $result = $conn->query($sql);
        return $result;

        
}
function GetTestCart()
{
        
        $conn = getConnection();
        $sql =  'SELECT * FROM Test'; 
        $result = $conn->query($sql);
        return $result;

        
}
function Total()
{
            $conn = getConnection();
        $sql =  'SELECT SUM(Value) AS total FROM Test'; 
        $result = $conn->query($sql);
        return $result;
}
function GetCartItem()
{
        
        $conn = getConnection();
        $sql =  "SELECT * FROM Cart WHERE id='$rs[id]'"; 
        $result = $conn->query($sql);
        return $result;

        
}
function SaveCart($rs)
{
        $conn = getConnection();
        $rs = EscapeRS($conn, $rs);
        $result = $conn->query("UPDATE Cart SET Value='$rs[Value]', Category=$rs[Category], ImageLink=$rs[ImageLink]  WHERE id=$rs[id]");
        $error = $conn->error;
        $conn->close();
        
        return $error ? array('Server Error' => $error) : Null;
}
function EscapeRS($conn, $rs)
{
        $cleanRs = array();
        foreach($rs as $key => $value)
        {
                $cleanRs[$key] = $conn->real_escape_string($value);
        }
        return $cleanRs;
}
function CreateCart($rs)
{
        $conn = getConnection();
        $rs = EscapeRS($conn, $rs);
        $result = $conn->query(
                "INSERT INTO Cart (ImageLink, Category, Value) "
        .       "VALUES ('$rs[ImageLink]', '$rs[Category]', $rs[Value])");
        $error = $conn->error;
        if(!$error)$rs['id'] = $conn->insert_id;
        $conn->close();
        return $error ? array('Server Error' => $error) : Null;
}
function NewCart()
{
        return array('ImageLink'=>'','Category'=>'','Value'=>'');

}
function DeleteCart($id)
{
        $conn = getConnection();
       $result = $conn->query("DELETE FROM Cart WHERE id=$id");
        $error = $conn->error;
        $conn->close();
        
        return $error ? array('Server Error' => $error) : Null;
}

function ValidateCart($rs)
{
        $errors = Null;
        return $errors;
} 
function AddCart($rs)
{
        $conn=getConnection();
        $rs = EscapeRS($conn, $rs);
        $result = $conn->query(
                "INSERT INTO Test (ItemId,ImageLink,Value, UserId) "
        .       "VALUES ('$rs[id]','$rs[ImageLink]', '$rs[Value]', GetUserID())");
        $error = $conn->error;
        if(!$error)$rs['id'] = $conn->insert_id;
        $conn->close();
        return $error ? array('Server Error' => $error) : Null;
}

